﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using DTO;
using System.Data.Sql;

namespace DAO
{
    public class ChiTietDauThauDAO
    {
        DataProvider provider;

        public ChiTietDauThauDAO()
        {
            provider = new DataProvider();
        }

        public int ThemChiTietDauThau(ChiTietDauThau chiTietDauThau)
        {
            string sqlStatement = "INSERT INTO ChiTietDauThau(MaDauThau, MaTaiKhoan, ThoiGianDauThau, SoTienRaThau, DaXoa) VALUES "
                + "('" + chiTietDauThau.MaDauThau + "', '" + chiTietDauThau.MaTaiKhoan
                + "', '" + chiTietDauThau.ThoiGianDauThau + "', " + chiTietDauThau.SoTienRaThau + ", 0)";

            return provider.ExecuteNonQuery(sqlStatement);
        }

        public int CapNhatChiTietDauThau(ChiTietDauThau chiTietDauThau)
        {
            string sqlStatement = "UPDATE ChiTietDauThau SET MaDauThau='" + chiTietDauThau.MaDauThau
                + "', MaTaiKhoan='" + chiTietDauThau.MaTaiKhoan + "', ThoiGianDauThau='" + chiTietDauThau.ThoiGianDauThau
                + "', SoTienRaThau=" + chiTietDauThau.SoTienRaThau + " WHERE MaCTDauThau='" + chiTietDauThau.MaCTDauThau + "'";
            
            return provider.ExecuteNonQuery(sqlStatement);
        }

        public int XoaChiTietDauThau(ChiTietDauThau chiTietDauThau)
        {
            string sqlStatement = "UPDATE ChiTietDauThau SET DaXoa=1 WHERE MaCTDauThau='" + chiTietDauThau.MaCTDauThau + "'";

            return provider.ExecuteNonQuery(sqlStatement);
        }
        
        public ArrayList LayDanhSachChiTietDauThauTheoMaDauThau(DauThau dauThau)
        {
            ArrayList arrChiTietDauThau = new ArrayList();
            try
            {
                string sqlStatement = "SELECT * FROM ChiTietDauThau WHERE MaDauThau='" + dauThau.MaDauThau + "' ORDER BY ThoiGianDauThau DESC";
                DataTable dt = new DataTable();
                dt = provider.ExecuteQuery(sqlStatement);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ChiTietDauThau item = new ChiTietDauThau();

                    item.MaCTDauThau = Int32.Parse(dt.Rows[i]["MaCTDauThau"].ToString());
                    item.MaDauThau = dt.Rows[i]["MaDauThau"].ToString();
                    item.MaTaiKhoan = dt.Rows[i]["MaTaiKhoan"].ToString();
                    item.ThoiGianDauThau = DateTime.Parse(dt.Rows[i]["ThoiGianDauThau"].ToString());
                    item.SoTienRaThau = float.Parse(dt.Rows[i]["SoTienRaThau"].ToString());
                    item.DaXoa = bool.Parse(dt.Rows[i]["DaXoa"].ToString());

                    arrChiTietDauThau.Add(item);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error: " + ex.Message);
            }
            return arrChiTietDauThau;
        }

        public ArrayList LayDanhSachChiTietDauThauTheoMaTaiKhoan(TaiKhoan taiKhoan)
        {
            ArrayList arrChiTietDauThau = new ArrayList();
            try
            {
                string sqlStatement = "SELECT * FROM ChiTietDauThau WHERE MaTaiKhoan='" + taiKhoan.MaTaiKhoan + "'";
                DataTable dt = new DataTable();
                dt = provider.ExecuteQuery(sqlStatement);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ChiTietDauThau item = new ChiTietDauThau();

                    item.MaCTDauThau = Int32.Parse(dt.Rows[i]["MaCTDauThau"].ToString());
                    item.MaDauThau = dt.Rows[i]["MaDauThau"].ToString();
                    item.MaTaiKhoan = dt.Rows[i]["MaTaiKhoan"].ToString();
                    item.ThoiGianDauThau = DateTime.Parse(dt.Rows[i]["ThoiGianDauThau"].ToString());
                    item.SoTienRaThau = float.Parse(dt.Rows[i]["SoTienRaThau"].ToString());
                    item.DaXoa = bool.Parse(dt.Rows[i]["DaXoa"].ToString());

                    arrChiTietDauThau.Add(item);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error: " + ex.Message);
            }
            return arrChiTietDauThau;
        }
    }
}
